package cn.sunxyz.common.util;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.ConnectException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

/**
 * Created by sunxyz on 2017/3/3.
 */
public final class SiteFetchedUtil {

    private SiteFetchedUtil() {

    }

    private static final Optional<Document> OPTIONAL = Optional.empty();

    private static final Logger logger = LoggerFactory.getLogger(SiteFetchedUtil.class);

    private static Pattern domain = Pattern.compile("http[s]*://(([A-Za-z0-9-~.]*)*)");

    /**
     * 获取主网站
     * Created by sunxyz on 2017/3/3.
     */
    public static String getDomain(String url) {
        Matcher matcher = domain.matcher(url);
        matcher.find();
        return matcher.group(1);
    }

    public static Optional<Document> get(String url) {
        if (!isUrl(url)) {
            logger.error("链接格式不正确");
            return OPTIONAL;
        }
        try {
            //data(key,value)是该URL要求的参数
            //userAgent制定用户使用的代理类型
            //cookie带上cookie，如cookie("JSESSIONID","FDE234242342342423432432")
            //连接超时时间
            //post或者get方法
            Document doc = Jsoup.connect(url)
//                    .data("query", "Java")
//                    .userAgent("Mozilla")
                    .cookie("auth", "token")
                    .timeout(3000)
//                    .post();
                    .get();
            return Optional.of(doc);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return OPTIONAL;
    }

    public static String downloadICO(String domain) {
        return downloadICO(domain, "d://ico//");
    }

    public static String downloadICO(String domain, String rootPath) {
        String url = "http://" + domain + "/favicon.ico";
        String filename = "favicon.ico";
        String strs[] = domain.split("\\.");
        StringBuffer path = new StringBuffer();
        int len = strs.length;
        for (int i = len - 1; i > -1; i--) {
            path.append(strs[i]).append("/");
        }
        download(url, filename, rootPath + path.toString());
        return path + filename;
    }

    /**
     * 根据路径 下载图片 然后 保存到对应的目录下
     *
     * @param urlString
     * @param filename
     * @param savePath
     * @return
     * @throws Exception
     */
    public static void download(String urlString, String filename, String savePath) {

        InputStream is = null;
        OutputStream os = null;
        try {
            // 构造URL
            URL url = new URL(urlString);
            // 打开连接
            URLConnection con = url.openConnection();
            //防止屏蔽程序抓取而返回403错误
            con.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            //设置请求的路径
            con.setConnectTimeout(6 * 1000);
            // 输入流
            is = con.getInputStream();
            // 1K的数据缓冲
            byte[] bs = new byte[1024];
            // 读取到的数据长度
            int len;
            // 输出的文件流
            File dir = new File(savePath);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            os = new FileOutputStream(dir.getPath() + "\\" + filename);
            // 开始读取
            while ((len = is.read(bs)) != -1) {
                os.write(bs, 0, len);
            }
        } catch (MalformedURLException e) {
            logger.error("MalformedURLException : {}", e.getMessage());
        } catch (FileNotFoundException e) {
            logger.error("FileNotFoundException :{}", e.getMessage());
        } catch (IOException e) {
            logger.error("IOException {}", e.getMessage());
        } finally {
            // 完毕，关闭所有链接
            try {
                os.close();
                is.close();
            } catch (NullPointerException e) {
                logger.error("favicon.ico 抓取失败 => {}", urlString);
                return;
            } catch (IOException e) {
                logger.error("favicon.ico 抓取失败 => {}", urlString);
                return;
            }
        }
        logger.info("favicon.ico 抓取成功 => {}", urlString);
    }

    private static Pattern pattern = Pattern
            .compile("^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\\/])+$");

    private static boolean isUrl(String url) {
        return pattern.matcher(url).matches();
    }
}
